<?php

namespace app\index\validate;

use think\Validate;

/**
 * Notes: 验证表彰
 * User: Lionel
 * DateTime: 2020/10/12 11:14
 */
class Sub extends Validate
{
//验证规则
    protected $rule = [
        'username' => 'require',
        'card_num' => 'require|alphaNum|checkCard',
        'age' => 'require|number',
        'com_name' => 'require',
        'mobile' => 'require|checkMobile|unique:person',
        'renshu' => 'require|number',
        'head_pic' => 'require',
    ];

    //错误消息
    protected $message = [
        'username' => '用户名不用为空',
        'card_num' => '身份证号格式不正确',
        'age' => '年龄必须为有效数字 ',
        'com_name' => '企业名称不能为空',
        'mobile.require' => '联系方式必填',
        'mobile.checkMobile' => '手机号格式不正确',
        'mobile.unique' => '该手机号已报名',
        'renshu' => '同行人数必须为有效数字 ',
        'head_pic' => '图片必传 ',
    ];
    function checkMobile($value, $rule, $data)
    {

        if (preg_match("/^1[345789]\d{9}$/", $value)) {
            return true;
        } else {
            return '手机号格式不正确';
        }
    }
    function checkCard($value, $rule, $data)
    {
        if(strlen($value) != 18){
            return '身份证格式不正确';
        }else{
            return true;
        }

    }
    //验证场景
    protected $scene = [
        'add' => ['username', 'card_num','age','com_name','mobile','renshu','head_pic'],
    ];
}
